﻿#include "mainwindow.h"
#include <QAction>
#include <QMenuBar>
#include <QMessageBox>
#include <QStatusBar>
#include <QToolBar>
#include <QTextEdit>
#include <QTextStream>
#include <QFile>
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent) {

    openAction = new QAction(QIcon(":/images/fold_icon"), tr("&Open..."), this);
    openAction->setShortcut(QKeySequence::Open);
    openAction->setStatusTip(tr("Open an existing text file."));

    saveAction = new QAction(QIcon(":/images/save_icon"), tr("&Save .."), this);
    saveAction->setShortcut(QKeySequence::Save);
    saveAction->setStatusTip("Save a text file.");

    QMenu *file = menuBar()->addMenu(tr("&File"));
    file->addAction(openAction);
    file->addAction(saveAction);

    QToolBar *toolBar = addToolBar(tr("&File"));
    toolBar->addAction(openAction);
    toolBar->addAction(saveAction);

    statusBar();

    textEdit = new QTextEdit(this);
    setCentralWidget(textEdit);
    connect(textEdit, &QTextEdit::textChanged, [=]() {
        this->setWindowModified(true);
    });

    setWindowTitle("Text Editor [*]");

    // 连接信号槽
    connect(openAction, &QAction::triggered, this, &MainWindow::open);
    connect(saveAction, &QAction::triggered, this, &MainWindow::save);
}


MainWindow::~MainWindow() {

}

void MainWindow::closeEvent(QCloseEvent *event) {
    if (isWindowModified()) { // 文本框内容是否进行过修改
        bool exit = QMessageBox::question(this,
                                          tr("Quit"),
                                          tr("Are you sure to quit this application?"),
                                          QMessageBox::Yes | QMessageBox::No,
                                          QMessageBox::No) == QMessageBox::Yes;
        if (exit) {
            event->accept();
        }
        else {
            event->ignore();
        }
    }
    else {
        event->accept();
    }
}

void MainWindow::open() {
    QString path = QFileDialog::getOpenFileName(this,
                                                tr("Open File"), // 打开的文件对话框的标题
                                                ".", // 基准目录
                                                tr("Text Files(*.txt)")); // 指定文件类型 - filter
    if (!path.isEmpty()) {
        QFile file(path);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { // 防止读入时有其他进程对在读文件进行修改
            QMessageBox::warning(this, tr("Read File"),
                tr("Cannot open file:\n%1").arg(path));
            return;
        }
        QTextStream in(&file);
        textEdit->setText(in.readAll());
        file.close();
    }
}

void MainWindow::save() {
    QString path = QFileDialog::getOpenFileName(this,
                                                tr("Save content to specific file"),
                                                ".",
                                                tr("Text Files(*.txt)"));
    if (!path.isEmpty()) {
        QFile file(path);
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { // 防止写入时有其他进程正在读取文件
            QMessageBox::warning(this,
                                 tr("Read File"),
                                 tr("Cannot open file:\n%1").arg(path));
            return;
        }
        QTextStream out(&file);
        out << textEdit->toPlainText(); // 通过文本流将富文本框内的内容写入至文件中
        file.close();
        QMessageBox msg(this);
        msg.setText(tr("Saved successfully."));
        msg.setDetailedText(tr("saved to ") + path);
        msg.setStandardButtons(QMessageBox::Ok);
        msg.exec();
        this->setWindowModified(false);
    }
}
